package com.psm.model;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.mchange.v1.util.ArrayUtils;
import com.psm.util.MD5;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by cheam on 15/6/19.
 */
public class SysDepartment extends Model<SysDepartment> {

    public static String tableName="sys_department";

    public static final SysDepartment me = new SysDepartment();


    /**
     * 所有 sql 与业务逻辑写在 Model 或 Service 中，不要写在 Controller 中，养成好习惯，有利于大型项目的开发与维护
     */
    public Page<SysDepartment> pageinate(int pageNumber, int pageSize) {

        return paginate(pageNumber, pageSize,"select * ", "from "+tableName);
    }

    public List<Record> queryAll(String dep_id) {

        List<Record> dataList = Db.find("select id,dep_name cname, parent_id pid from "+this.tableName+" where FIND_IN_SET(id,getDepChildList("+dep_id+"))");

        return dataList;
    }

    public List<Record> queryAllAndClass(String dep_id) {

        List<Record> dataList = Db.find("select id,dep_name cname, parent_id pid, 1 type from "+this.tableName+" where FIND_IN_SET(id,getDepChildList("+dep_id+"))");

        return dataList;
    }

    public Record queryMinPid(String dep_id) {

        Record dataList = Db.findFirst("select min(parent_id) pid from "+this.tableName+" where FIND_IN_SET(id,getDepChildList("+dep_id+"))");

        return dataList;
    }

    public Record qryLoginUser(Long Id){
        Record user = Db.findFirst("select * from "+tableName+" where id=?",Id);
        return user;
    }




}
